﻿using System;
using System.IO;
using System.Windows.Threading;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Diagnostics;
using System.Windows;

namespace Model
{
    public class DirectorySearchModel : INotifyPropertyChanged
    { public static RelayCommand OpenCommand { get; set; }
        private string _selectedPath;
        private ObservableCollection<string> _filesCollection = new ObservableCollection<string>();
        public string FolderPath { get; set; }
        public string Pattern { get; set; }
        private string _showPath;
        
        void ShowCurrentPath(string path)
        {
            ShowPath = path;
        }

        public string SelectedPath
        {
            get { return _selectedPath; }
            set
            {
                _selectedPath = value;
                OnPropertyChanged("SelectedPath");
            }
        }

        private string _defaultPath;

        public DirectorySearchModel()
        {
            RegisterCommands();
        }

        public DirectorySearchModel(string defaultPath)
        {
            _defaultPath = defaultPath;
            RegisterCommands();
        }

        private void RegisterCommands()
        {
            OpenCommand = new RelayCommand(ExecuteOpenFileDialog);
        }

        private void ExecuteOpenFileDialog(object o)
        {
            
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            string path = AppDomain.CurrentDomain.BaseDirectory;
            dlg.SelectedPath = path;
            System.Windows.Forms.DialogResult result = dlg.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                FolderPath = dlg.SelectedPath;
                Pattern= "*.*";

                
            }
        }

        void AddFileToCollection(string file)
        {
            if(Utility.Utility.CheckDirectory(file))

            {
            _filesCollection.Add(file);
            }
        }
        private void Search()
        {

            Search(FolderPath, Pattern);
        }
        public void Search(string path, string pattern)
        {
            
            if (Application.Current.Dispatcher.CheckAccess())
                ShowPath = path;
            else
                Application.Current.Dispatcher.Invoke(
                  new Action<string>(ShowCurrentPath), DispatcherPriority.Background, new string[] { path }
                );
            string[] files = Directory.GetFiles(path, pattern);
            foreach (string file in files)
            {
                if (Application.Current.Dispatcher.CheckAccess())
                    FileCollection.Add(file);
                else
                    Application.Current.Dispatcher.Invoke(new Action<string>(AddFileToCollection), DispatcherPriority.Background,
                      new string[] { file }
                    );
            }
            string[] dirs = System.IO.Directory.GetDirectories(path);
            foreach (string dir in dirs)
                Search(dir, pattern);
        }

        public string ShowPath
        {
            get
            {
                return _showPath;
            }
            set
            {
                _showPath = value;
                OnPropertyChanged("ShowPath");
            }
        }
        public ObservableCollection<string> FileCollection
        {
            get
            {
                return _filesCollection;
            }
            set
            {
                _filesCollection = value;
                OnPropertyChanged("FileCollection");
            }
        }
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));

            }
        }
        public ICommand SearchCommand
        {
            get
            {
                if (_searchCommand == null)
                {
                    _searchCommand = new AsyncDelegateCommand(
                         () => Search(),
                           error: (ex) => Debug.WriteLine(ex.Message));

                }
                return _searchCommand;
            }
        }
        private ICommand _searchCommand;
        public event PropertyChangedEventHandler PropertyChanged;
    }
}